//
//  ReadViewController.m
//  New
//
//  Created by 才诗琦 on 16/1/17.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "ReadViewController.h"

@interface ReadViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;

@end

@implementation ReadViewController
- (void)dealloc
{
    
    [super dealloc];
    [_tableView release];
    [_dataArr release];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //tableView
    [self createTableView];
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    //注册cell
    [_tableView registerClass:[ReadCell class] forCellReuseIdentifier:@"cell"];
    
   
    
    //处理数据
    [self data];
    //内存管理
    [_tableView release];
    
    
    
    
}
//处理数据
- (void)data
{
    NSString *str = @"http://c.m.163.com/nc/article/list/T1348649776727/0-20.html";
    
    [AFNModel GETWithHandler:str completion:^(id result) {
        self.dataArr = [ReadModel modelHandlerWithArray:result[@"T1348649776727"]];
        [_tableView reloadData];
    }];
    
}
//cell设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadModel *qa = _dataArr[indexPath.row];
    
        ReadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell.imagView sd_setImageWithURL:[NSURL URLWithString:qa.imgsrc]];
    cell.titleLabel.text = qa.title;
    cell.digestLabel.text = qa.digest;
    cell.replayCount.text = [[qa.replyCount stringValue] stringByAppendingString:@"跟帖"];
    
    
        return cell;


    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadModel *r = _dataArr[indexPath.row];

    CGFloat h = [ReadCell heightWithText:r.digest];
        return  h + 100;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
